import QtQuick 2.0
import Process 1.0
import FileIO 1.0

Item {
    id: downloader

    // in
    property string downloadDir
    property bool multiConnections: false
    property bool limitDownload: false
    property string limitDownloadValue: "1M"

    // out
    readonly property alias isRunning: process.isRunning
    property var progress
    property bool canceled: false

    property string archiveCompletedPath

    signal finished()
    signal error(string errors)

    Process {
        id: process

        property string errors: ""

        onFinished: {
            console.log("Download exit code: " + code)
            if (code === 0) {
                FileIO.writeTextFile(archiveCompletedPath, "")
                downloader.finished()
            } else {
                downloader.error(errors)
            }
        }

        onOutputRead: {
            let lines = output.split("\n")
            for (let line of lines) {
                // "[#6b754a 249MiB/5.4GiB(4%) CN:1 DL:6.4MiB ETA:13m46s]\n"
                let groups = line.match(/\s([^\/]+)\/([^\/]+)\((\d+)%?\).*DL:([^\s]+).*ETA:([^\s\]]+)/)
                // 0: " 249MiB/5.4GiB(4%) CN:1 DL:6.4MiB ETA:13m46s"
                // 1: "249MiB"
                // 2: "5.4GiB"
                // 3: "4%"
                // 4: "6.4MiB"
                // 5: "13m46s"
                if (groups) {
                    progress = {
                        downloaded: groups[1],
                        total: groups[2],
                        percent: Number(groups[3]),
                        speed: groups[4],
                        estimated: groups[5]
                    }
                }

                if (line.match(/ERROR/i)) {
                    errors += "\n" + line
                }
            }
        }
    }

    function download(url, fileName, md5) {
        canceled = false

        let filePath = downloadDir + fileName
        archiveCompletedPath = filePath + ".completed"

        process.errors = ""

        if (FileIO.isFileExists(archiveCompletedPath)) {
            // patch archive was already downloaded but something went wrong when applying it
            ui.log(fileName + " " + qsTr("is already downloaded"))
            finished()
            return
        } else {
           ui.log(qsTr("Starting downloading: ") + fileName)
            FileIO.createDirectory(downloadDir)
            process.workingDirectory = downloadDir
            var args = ["-oL", "aria2c", "-c", url, "--enable-color=false", `--dir=${downloadDir}`, `--out=${fileName}`]
            if (md5) {
                args.push(`--checksum=md5=${md5}`)
            }
            if (multiConnections) {
                args.push("-x4")
            }
            if (limitDownload) {
                args.push("--max-download-limit=" + limitDownloadValue)
            }

            process.start("stdbuf", args)
        }

        progress = {
            downloaded: "",
            total: "",
            percent: 0,
            speed: "",
            estimated: ""
        }
    }

    function cancel() {
        canceled = true
        process.terminate()
    }

}
